import { defineStore } from "pinia"
export const useBaseStore = defineStore({
	id: "store",
	state: () => ({
		userInfo: null, // 用户信息
	}),
	getters: {
		/**
		 * 返回用户信息
		 * @returns 用户信息
		 */
		handleGetUserInfo() {
			return this.userInfo
		},
		/**
		 * 返回用户是否为管理员
		 * @returns Boolean 是否管理员
		 */
		handleGetUserAdmin() {
			const nowTeamId = this.userInfo.now_team_id
			const teamArr = this.userInfo.teams
			const nowTeam = teamArr.find(x => {
				return x.id === nowTeamId
			}) || undefined;
			return nowTeam && nowTeam.auth.is_admin == 1 || nowTeam.auth.is_super == 1 || false
		}
	},
	actions: {
		/**
		 * 设置 用户信息
		 * @param {Object} user
		 */
		handleSetUserInfo(user) {
			this.userInfo = user
		},
		/**
		 * 给定一个id返回是不是当前登录的用户
		 * @param {string} sn
		 */
		handleIsCurrentUser(sn) {
			return this.userInfo.sn === sn
		},
	},
	persist: {
		// 指定要持久化的属性名称  默认全部
		// paths: ['activeTabsKey'],
		// 指定存储的键名称，默认是“pinia-state”
		key: 'calendarAgency',
		// 指定存储引擎，默认是`localStorage`
		storage: sessionStorage,

	},
})